#include "wordengine.h"
#include "ui_wordengine.h"
#include <QDebug>
#include<QPainter>

WordEngine::WordEngine(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WordEngine)
{
    ui->setupUi(this);

    lb_cf[0] = ui->r1;
    lb_cf[1] = ui->r2;
    lb_cf[2] = ui->r3;
    lb_cf[3] = ui->r4;
    lb_cf[4] = ui->r5;
    lb_cf[5] = ui->r6;
    lb_cf[6] = ui->r7;

    lb_res[0] = ui->l1;
    lb_res[1] = ui->l2;
    lb_res[2] = ui->l3;
    lb_res[3] = ui->l4;
    lb_res[4] = ui->l5;
    lb_res[5] = ui->l6;
    lb_res[6] = ui->l7;

    this->setWindowTitle("诊断平台");
    img = resizPic(":/pic/bg1.bmp",100);
    ui->first->setStyleSheet(QString::fromUtf8("#first{border:1px solid black}"));
    ui->second->setStyleSheet(QString::fromUtf8("#second{border:1px solid black}"));
    ui->third->setStyleSheet(QString::fromUtf8("#third{border:1px solid black}"));
    ui->last->setStyleSheet(QString::fromUtf8("#last{border:1px solid black}"));
    ui->result->setStyleSheet(QString::fromUtf8("#result{border:1px solid black}"));
    memset(result,0,sizeof (result));
    memcpy(&score_t,&score,sizeof (score));
    this->resize(1300,993);
    //ui->centralWidget->setStyleSheet("color:white;");
    socket = new QTcpSocket(this);
    socket->connectToHost("yjr.nat123.cc",41345);
    tm = new QTimer(this);
    connect(tm,&QTimer::timeout,
            [=]()
    {
        if(is_confirm == false){
            this->hide();
            if(socket->isWritable())
            {
                socket->write("sss");
            }
        }
        else {
            if(this->isHidden() == true){
                this->show();
            }
        }
    });
    //tm->start(5000);
    connect(socket,&QTcpSocket::readyRead,this,&WordEngine::on_socket_read);

}

void WordEngine::on_socket_read()
{
    QByteArray arry = socket->readAll();
    if(arry.contains("RRR")){
        is_confirm = true;
    }
    if(arry.contains("EEE")){
        is_confirm = false;
    }
}
QPixmap WordEngine::resizPic(QString path, int position)
{
    //设置新的图片的透明度
    QPixmap pix1_(path);

    QPixmap temp(pix1_.size());
    temp.fill(Qt::transparent);

    QPainter p1(&temp);
    p1.setCompositionMode(QPainter::CompositionMode_Source);
    p1.drawPixmap(0, 0, pix1_);
    p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    //根据QColor中第四个参数设置透明度，此处position的取值范围是0～255
    p1.fillRect(temp.rect(), QColor(0, 0, 0, position));
    p1.end();

    pix1_ = temp;
    return pix1_;
}

void WordEngine::paintEvent(QPaintEvent *)
{
    QPixmap imgt;
    imgt = img.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio);
    QPainter p(this);
    p.drawPixmap(0,0,imgt);

}
void WordEngine::update_sta(int item, int index, int sta)
{
    item--;
    index--;
    /* 设置对应位标记 */
    score_t[item][index].set = sta;
    /* 特殊处理 */
    if(sta == 0){
        if(item == 2){    /* 痛有定处”即可得分，不用管有没有选“胃痛拒按 */
            if( index == 0){
                if(ui->a_1_4->isChecked() == true || ui->d_1_2->isChecked() == true){
                    score_t[2][0].set = 1;
                }
            }
            else if (index == 2) {  /* “舌质淡暗或暗红”或“瘀点瘀斑”满足其一即可得分 */
                if(ui->c_1_4->isChecked() == true || ui->c_2_2->isChecked() == true){
                    score_t[2][2].set = 1;
                }
            }
            else {
                /* do nothing */
            }
        }
        else if ((item == 3) && (index == 0)) { /* “胀痛“或”胀满”满足其一即可得分 */
            if(ui->a_1_1->isChecked() == true || ui->a_2_4->isChecked() == true){
                score_t[3][0].set = 1;
            }
        }
        else if ((item == 6) && (index == 1)) { /* “口苦“或”口臭”满足其一即可得分 */
            if(ui->a_4_4->isChecked() == true || ui->a_4_5->isChecked() == true){
                score_t[6][1].set = 1;
            }
        }
        else {
            /* do nothing */
        }
    }
    else {
        if ((item == 6) && (index == 4)){    /* 需满足“黄苔“+（”厚苔“或”腻苔”）才可得分 */
            if((ui->c_4_2->isChecked() == false )\
                    || ((ui->c_5_1->isChecked() == false) && (ui->c_5_2->isChecked() == false))){
                score_t[6][4].set = 0;
            }
        }
    }

    memset(result,0,sizeof (result));
    /* 计算各病症得分 */
    for (uint8_t i=0;i<7;i++) {
        for (uint8_t j=0;j<config[i].num; j++) {
            if(score_t[i][j].set){
                result[i] += score_t[i][j].score;
//                qDebug()<<"i:"<<i<<"j:"<<j;
//                qDebug()<<"stat:"<<score_t[i][j].set<<"ret:"<<result[0];
            }
        }
        color_t[i] = (result[i]>=config[i].confirm_score)?"color:red;":"color:black;";
        lb_res[i]->setStyleSheet(color_t[i]);
        lb_res[i]->setText(QString("%1").arg(result[i]));
        if(color_t[i].contains("red")){
            lb_cf[i]->setText("确诊");
        }
        else {
            if(result[i]>=mul_t[i].dec){
                int ret = (double)(result[i]-mul_t[i].min)*mul_t[i].num*100;
                QString tes = QString("%1.%2%").arg(ret/100).arg(ret%100);
                lb_cf[i]->setText(tes);
            }
            else {
                lb_cf[i]->setText("---");;
            }
        }
    }
    ui->l1->setStyleSheet(color_t[0]);
    ui->l2->setStyleSheet(color_t[1]);
    ui->l3->setStyleSheet(color_t[2]);
    ui->l4->setStyleSheet(color_t[3]);
    ui->l5->setStyleSheet(color_t[4]);
    ui->l6->setStyleSheet(color_t[5]);
    ui->l7->setStyleSheet(color_t[6]);

    ui->l1->setText(QString("%1").arg(result[0]));
    ui->l2->setText(QString("%1").arg(result[1]));
    ui->l3->setText(QString("%1").arg(result[2]));
    ui->l4->setText(QString("%1").arg(result[3]));
    ui->l5->setText(QString("%1").arg(result[4]));
    ui->l6->setText(QString("%1").arg(result[5]));
    ui->l7->setText(QString("%1").arg(result[6]));
}

WordEngine::~WordEngine()
{
    delete ui;
}

void WordEngine::on_a_1_x_clicked()
{
    ui->a_1_t->clear();
    ui->a_1_1->setCheckState(Qt::CheckState::Unchecked);
    ui->a_1_2->setCheckState(Qt::CheckState::Unchecked);
    ui->a_1_3->setCheckState(Qt::CheckState::Unchecked);
    ui->a_1_4->setCheckState(Qt::CheckState::Unchecked);
    ui->a_1_5->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_a_1_1_stateChanged(int arg1)
{
    //qDebug()<<arg1;
    if(ui->a_1_x->isChecked() == true){
        //ui->a_1_x->setCheckState(Qt::CheckState::Unchecked);
    }
    update_sta(4,1, arg1);
}

void WordEngine::on_a_1_2_stateChanged(int arg1)
{
    if(ui->a_1_x->isChecked() == true){
        //ui->a_1_x->setCheckState(Qt::CheckState::Unchecked);
    }
    update_sta(5,1, arg1);
    update_sta(6,1, arg1);
}

void WordEngine::on_a_1_3_stateChanged(int arg1)
{
    update_sta(1,1, arg1);
    update_sta(2,1, arg1);
}

void WordEngine::on_a_1_4_stateChanged(int arg1)
{
    update_sta(3,1, arg1);
}

void WordEngine::on_a_1_5_stateChanged(int arg1)
{

}

void WordEngine::on_a_2_x_clicked()
{
    ui->a_2_t->clear();
    ui->a_2_1->setCheckState(Qt::CheckState::Unchecked);
    ui->a_2_2->setCheckState(Qt::CheckState::Unchecked);
    ui->a_2_3->setCheckState(Qt::CheckState::Unchecked);
    ui->a_2_4->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_a_2_1_stateChanged(int arg1)
{
    update_sta(4,1, arg1);
    update_sta(1,2, arg1);
}

void WordEngine::on_a_2_2_stateChanged(int arg1)
{
    update_sta(7,1, arg1);
}

void WordEngine::on_a_2_3_stateChanged(int arg1)
{
    update_sta(5,2, arg1);
    update_sta(6,2, arg1);
}

void WordEngine::on_a_3_x_clicked()
{
    ui->a_3_t->clear();
    ui->a_3_1->setCheckState(Qt::CheckState::Unchecked);
    ui->a_3_2->setCheckState(Qt::CheckState::Unchecked);
    ui->a_3_3->setCheckState(Qt::CheckState::Unchecked);
    ui->a_3_4->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_a_3_1_stateChanged(int arg1)
{
    update_sta(4,2, arg1);
}

void WordEngine::on_a_3_2_stateChanged(int arg1)
{
    update_sta(5,4, arg1);
}

void WordEngine::on_a_3_3_stateChanged(int arg1)
{
    update_sta(2,2, arg1);
}

void WordEngine::on_a_4_x_clicked()
{
    ui->a_4_t->clear();
    ui->a_4_1->setCheckState(Qt::CheckState::Unchecked);
    ui->a_4_2->setCheckState(Qt::CheckState::Unchecked);
    ui->a_4_3->setCheckState(Qt::CheckState::Unchecked);
    ui->a_4_4->setCheckState(Qt::CheckState::Unchecked);
    ui->a_4_5->setCheckState(Qt::CheckState::Unchecked);
    ui->a_4_6->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_a_4_1_stateChanged(int arg1)
{
    update_sta(1,3, arg1);
}

void WordEngine::on_a_4_2_stateChanged(int arg1)
{
    update_sta(6,3, arg1);
}

void WordEngine::on_a_4_3_stateChanged(int arg1)
{
    update_sta(6,4, arg1);
}

void WordEngine::on_a_4_4_stateChanged(int arg1)
{
    update_sta(5,3, arg1);
    update_sta(7,2, arg1);
}

void WordEngine::on_a_4_5_stateChanged(int arg1)
{
    update_sta(7,2, arg1);
}

void WordEngine::on_b_1_x_clicked()
{
    ui->b_1_t->clear();
    ui->b_1_1->setCheckState(Qt::CheckState::Unchecked);
    ui->b_1_2->setCheckState(Qt::CheckState::Unchecked);
    ui->b_1_3->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_b_2_x_clicked()
{
    ui->b_2_t->clear();
    ui->b_2_1->setCheckState(Qt::CheckState::Unchecked);
    ui->b_2_2->setCheckState(Qt::CheckState::Unchecked);
    ui->b_2_3->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_b_3_x_clicked()
{
    ui->b_3_t->clear();
    ui->b_3_1->setCheckState(Qt::CheckState::Unchecked);
    ui->b_3_2->setCheckState(Qt::CheckState::Unchecked);
    ui->b_3_3->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_b_4_x_clicked()
{
    ui->b_4_t->clear();
    ui->b_4_1->setCheckState(Qt::CheckState::Unchecked);
    ui->b_4_2->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_b_1_1_stateChanged(int arg1)
{
    update_sta(4,4, arg1);
}

void WordEngine::on_b_1_2_stateChanged(int arg1)
{
    update_sta(2,4, arg1);
}

void WordEngine::on_b_2_1_stateChanged(int arg1)
{
    update_sta(5,5, arg1);
}

void WordEngine::on_b_2_2_stateChanged(int arg1)
{
    update_sta(4,5, arg1);
}

void WordEngine::on_b_3_1_stateChanged(int arg1)
{
    update_sta(1,4, arg1);
}

void WordEngine::on_b_3_2_stateChanged(int arg1)
{
    update_sta(1,5, arg1);
}

void WordEngine::on_b_4_1_stateChanged(int arg1)
{
    update_sta(3,2, arg1);
}

void WordEngine::on_c_1_1_stateChanged(int arg1)
{

}

void WordEngine::on_c_1_2_stateChanged(int arg1)
{
    update_sta(2,7, arg1);
    update_sta(1,7, arg1);
}

void WordEngine::on_c_1_3_stateChanged(int arg1)
{
    update_sta(7,5, arg1);
    update_sta(6,6, arg1);
    update_sta(5,6, arg1);
}

void WordEngine::on_c_1_4_stateChanged(int arg1)
{
    update_sta(3,3, arg1);
}

void WordEngine::on_c_2_1_stateChanged(int arg1)
{
    update_sta(6,8, arg1);
}

void WordEngine::on_c_2_2_stateChanged(int arg1)
{
    update_sta(3,3, arg1);
}

void WordEngine::on_c_2_3_stateChanged(int arg1)
{
    update_sta(3,4, arg1);
}

void WordEngine::on_c_3_x_clicked()
{
//    ui->c_3_1->setCheckState(Qt::CheckState::Unchecked);
//    ui->c_3_2->setCheckState(Qt::CheckState::Unchecked);
//    ui->c_3_3->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_c_3_1_stateChanged(int arg1)
{
    update_sta(1,8, arg1);
}

void WordEngine::on_c_3_2_stateChanged(int arg1)
{
    update_sta(6,7, arg1);
}

void WordEngine::on_c_4_2_stateChanged(int arg1)
{
    update_sta(5,7, arg1);
    update_sta(7,5, arg1);
}

void WordEngine::on_c_5_1_stateChanged(int arg1)
{
    update_sta(7,5, arg1);
}

void WordEngine::on_c_5_2_stateChanged(int arg1)
{
    update_sta(7,5, arg1);
}

void WordEngine::on_c_5_4_stateChanged(int arg1)
{
    update_sta(6,9, arg1);
}

void WordEngine::on_e_1_1_stateChanged(int arg1)
{
    update_sta(4,6, arg1);
}

void WordEngine::on_e_1_2_stateChanged(int arg1)
{
    update_sta(6,10, arg1);
}

void WordEngine::on_e_1_3_stateChanged(int arg1)
{
    update_sta(7,6, arg1);
}

void WordEngine::on_e_1_4_stateChanged(int arg1)
{
    update_sta(2,8, arg1);
    update_sta(1,9, arg1);
}

void WordEngine::on_e_1_5_stateChanged(int arg1)
{
    update_sta(3,5, arg1);
}

void WordEngine::on_d_1_x_clicked()
{

}

void WordEngine::on_d_1_1_stateChanged(int arg1)
{
    update_sta(2,5, arg1);
}

void WordEngine::on_d_1_2_stateChanged(int arg1)
{
    update_sta(3,1, arg1);
}

void WordEngine::on_d_1_3_stateChanged(int arg1)
{
    update_sta(1,6, arg1);
}

void WordEngine::on_d_1_4_stateChanged(int arg1)
{
    update_sta(4,4, arg1);
}

void WordEngine::on_d_1_5_stateChanged(int arg1)
{
    update_sta(2,6, arg1);
}

void WordEngine::on_bt_clear_clicked()
{
    /* 清除A组 */
    ui->a_1_x->click();
    ui->a_1_x->click();
    ui->a_2_x->click();
    ui->a_3_x->click();
    ui->a_4_x->click();
    ui->a_5_x->click();
    /* 清除B组 */
    ui->b_1_x->click();
    ui->b_2_x->click();
    ui->b_3_x->click();
    ui->b_4_x->click();

    /* 清除C_3组 */
    ui->c_3_x->setCheckState(Qt::CheckState::Checked);
    /* 清除C组 */
    ui->c_1_1->setCheckState(Qt::CheckState::Unchecked);
    ui->c_1_2->setCheckState(Qt::CheckState::Unchecked);
    ui->c_1_3->setCheckState(Qt::CheckState::Unchecked);
    ui->c_1_4->setCheckState(Qt::CheckState::Unchecked);
    ui->c_1_5->setCheckState(Qt::CheckState::Unchecked);
    ui->c_1_t->clear();

    ui->c_2_1->setCheckState(Qt::CheckState::Unchecked);
    ui->c_2_2->setCheckState(Qt::CheckState::Unchecked);
    ui->c_2_3->setCheckState(Qt::CheckState::Unchecked);
    ui->c_2_4->setCheckState(Qt::CheckState::Unchecked);
    ui->c_2_t->clear();

    ui->c_4_1->setCheckState(Qt::CheckState::Unchecked);
    ui->c_4_2->setCheckState(Qt::CheckState::Unchecked);
    ui->c_4_3->setCheckState(Qt::CheckState::Unchecked);
    ui->c_4_t->clear();

    ui->c_5_1->setCheckState(Qt::CheckState::Unchecked);
    ui->c_5_2->setCheckState(Qt::CheckState::Unchecked);
    ui->c_5_3->setCheckState(Qt::CheckState::Unchecked);
    ui->c_5_4->setCheckState(Qt::CheckState::Unchecked);
    ui->c_5_5->setCheckState(Qt::CheckState::Unchecked);
    ui->c_5_t->clear();
    /* 清除D组 */
    ui->d_1_1->setCheckState(Qt::CheckState::Unchecked);
    ui->d_1_2->setCheckState(Qt::CheckState::Unchecked);
    ui->d_1_3->setCheckState(Qt::CheckState::Unchecked);
    ui->d_1_4->setCheckState(Qt::CheckState::Unchecked);
    ui->d_1_5->setCheckState(Qt::CheckState::Unchecked);
    ui->d_1_x->setCheckState(Qt::CheckState::Unchecked);
    ui->d_1_t->clear();

    /* 清除E组 */
    ui->e_1_1->setCheckState(Qt::CheckState::Unchecked);
    ui->e_1_2->setCheckState(Qt::CheckState::Unchecked);
    ui->e_1_3->setCheckState(Qt::CheckState::Unchecked);
    ui->e_1_4->setCheckState(Qt::CheckState::Unchecked);
    ui->e_1_5->setCheckState(Qt::CheckState::Unchecked);
    ui->e_1_6->setCheckState(Qt::CheckState::Unchecked);
    ui->e_1_t->clear();
}


void WordEngine::on_c_3_x_stateChanged(int arg1)
{
    if(arg1){
        ui->c_3_1->setCheckState(Qt::CheckState::Unchecked);
        ui->c_3_2->setCheckState(Qt::CheckState::Unchecked);
        ui->c_3_3->setCheckState(Qt::CheckState::Unchecked);
        ui->c_3_x->setCheckState(Qt::CheckState::Unchecked);
        ui->c_3_t->clear();
    }
}

void WordEngine::on_c_4_1_stateChanged(int arg1)
{

}

void WordEngine::on_a_5_x_clicked()
{
    ui->a_5_t->clear();
    ui->a_5_1->setCheckState(Qt::CheckState::Unchecked);
    ui->a_5_2->setCheckState(Qt::CheckState::Unchecked);
    ui->a_5_3->setCheckState(Qt::CheckState::Unchecked);
    ui->a_5_4->setCheckState(Qt::CheckState::Unchecked);
}

void WordEngine::on_a_5_1_stateChanged(int arg1)
{
    update_sta(6,5, arg1);
}

void WordEngine::on_a_5_2_stateChanged(int arg1)
{
    update_sta(7,3, arg1);
}

void WordEngine::on_a_5_3_stateChanged(int arg1)
{
    update_sta(2,3, arg1);
}
